#version 460


layout(points) in;
//layout(triangle_strip, max_vertices=128) out;
layout(line_strip, max_vertices=64) out;
//layout(points, max_vertices=64) out;


#include "../../lib/project_matrix.glsl"






//====================== 顶点阶段输入数据 =====================
in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 3) in VS_GS_VERTEX {
	uint	InstanceIndex;
	float	VBoxSize;
}vertex_in[];

//======================= 几何阶段输出数据 ========================
/*layout(location = 1) out VS_FS_VERTEX {
	vec4		gColor;
	flat ivec3	vUV_Offset;
	flat ivec2	vUV_Bound;
};*/

layout(location = 0) out vec4 gColor;


void main(void){
	// 位置  大小  对齐方式  深度
	mat4 mat = mat_Model[vertex_in[0].InstanceIndex];
	//摄像机逆矩阵
	mat4 mr = transpose(mat_ProjecView[1]);
	mr[3] = vec4(0,0,0,1);

	vec4 vA = gl_in[0].gl_Position;
	//vec4 vB = vA;
	float radius = gl_in[0].gl_PointSize;

	uint dim = 24;
	float angle = radians(360/48);
	for(uint i=0; i<dim; ++i) {
		uint angle_step = i*2;
		
		//mat4 mr = inverse(mat_ProjecView[1]);
		
		

		vec4 v = vec4(0);
		v.x += sin(angle_step*angle) * radius;
		v.y += cos(angle_step*angle) * radius;
		v = mr * v + vA;

		gl_Position = mat_ProjecView[0] * mat * (v);
		EmitVertex();


		//mr = transpose(mat_ProjecView[1]);
		v = vec4(0);
		v.x += sin((angle_step + 1) * angle) * radius;
		v.y += cos((angle_step + 1) * angle) * radius;
		v = mr * v + vA;

		gl_Position = mat_ProjecView[0] * mat * (v);
		gColor = vec4(1,1,1,1);
		EmitVertex();
		
		EndPrimitive();
	}
	
	//画中心十字
	vec4 v = vec4(-radius, 0, 0, 0);
	v = mr * v + vA;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(1,1,1,1);
	EmitVertex();
	v = vec4(radius, 0, 0, 0);
	v = mr * v + vA;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(1,1,1,1);
	EmitVertex();
	EndPrimitive();

	v = vec4(0, -radius, 0, 0);
	v = mr * v + vA;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(1,1,1,1);
	EmitVertex();
	v = vec4(0, radius, 0, 0);
	v = mr * v + vA;
	gl_Position = mat_ProjecView[0] * mat * v;
	gColor = vec4(1,1,1,1);
	EmitVertex();
	EndPrimitive();
}



 